<?php
/*	Created by Alexander Alexandrov aka SS
			hazg@mail.ru				*/
define('SSLIB_FileUpload', TRUE);

class FileUpload
{
	private $tmpname,$key;
	function FileUpload($key='userfile')
	{
		$this->key = $key;
	}
	function SaveTemp()
	{
		
		$this->tmpname = Sslib::SiteRoot('tmp/'.RandomKey());
		 
		if(!isset($_FILES[$this->key]))
			throw new Err(__T('missing uploaded file'));
		if(!isset($_FILES[$this->key]['tmp_name']) || empty($_FILES[$this->key]['tmp_name']))
			throw new Err(__T('missing temporary file'));
		if(move_uploaded_file($_FILES[$this->key]['tmp_name'], $this->tmpname) === FALSE)
			throw new Err("cannot move file from temp");
		return $this->tmpname;
	}
	function IsUploaded()
	{
	    return
	    isset($_FILES[$this->key]) &&  
	    isset($_FILES[$this->key]['tmp_name']) && 
	    !empty($_FILES[$this->key]['tmp_name'])
	    ;
	}
	function __destruct()
	{
		/*if(isset($this->tmpname) && is_file($this->tmpname))
			unlink($this->tmpname);*/
	}
}